#ifndef OBJECTSTREEMODEL_H
#define OBJECTSTREEMODEL_H

#include <objectmanager.h>
#include <QAbstractItemModel>
#include <QPixmap>

class ObjectsTreeModel : public QAbstractItemModel
{
  Q_OBJECT
private:
  struct Item
  {
    enum Type
    {
      Type_Category,
      Type_Object,
      Type_Root
    };

    Type          type;
    Category      category;
    SceneObject   *object;
    Item          *parent;
    QList<Item*>  children;
    int GetIndex () const;
    void Debug (int indent = 0) const;
    void SingleDebug () const;
  };

  Item *_rootItem;

  QPixmap   _pmCategory;
  QPixmap   _pmObject;

  ObjectManager*  _manager;

private:
  Item* GetItem (Item* parent, const SceneObject *object);
  Item* GetItem (const Category& category);
  Item* GetItem (const SceneObject* object);

  void RemoveItem (Item* item);
  void ItemChanged (Item* item);

  void Debug (Item *item, int ind);
  QModelIndex CreateIndex (Item* item, int column = 0) const;


public:
  ObjectsTreeModel(ObjectManager* manager);

  bool IsCategory (const QModelIndex& index);
  bool IsSceneObject (const QModelIndex& index);
  Category* GetCatetory (const QModelIndex& index);
  SceneObject* GetSceneObject (const QModelIndex& index);

  virtual int columnCount (const QModelIndex& parent = QModelIndex()) const;
  virtual QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const;
  virtual QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const;
  virtual QModelIndex parent (const QModelIndex& index) const;
  virtual int rowCount (const QModelIndex& parent = QModelIndex()) const;

  void Debug ();

public slots:
  void AddObject (SceneObject* object);
  void RemoveObject (SceneObject* object);
  void MoveObject (SceneObject* object);
  void ChangeObject (SceneObject* object);
};

#endif // OBJECTSTREEMODEL_H
